import { Component, ElementRef } from '@angular/core';
import { NavController, NavParams, ModalController, App } from 'ionic-angular';
import * as echarts from 'echarts';
import { APP_MEMU } from '../../common/menu';
import { ApiProvider } from '../../providers/api/api';
import { NotificationProvider } from '../../providers/notification/notification';
import { PAGE_COUNT } from '../../app/app.constants';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  appMenu: any = [];
  chartOption: Object;
  pieChart: any;
  private warn_num:any;
  private now_in:any;
  private now_out:any;

  constructor( 
    private app: App,
    public navParams: NavParams, 
    private api: ApiProvider, 
    private notification: NotificationProvider,
    public modalCtrl: ModalController,
    public navCtrl: NavController, 
    private element: ElementRef) {
    this.appMenu = APP_MEMU; 
    
  }
  ionViewDidLoad() {
    //库存预警
    let items: Array<string>;
    let options1 = {
        start: 0,
        limit: PAGE_COUNT,
        where_sql:'v_sp_store_house.store_num >sp_limit.store_top or v_sp_store_house.store_num < sp_limit.store_lower ',
        where_type:'',
        where_value:''
    };
    let res = this.api.getListPage('sp_limit_house',options1).subscribe(res => {
      if(res['success']){
        items = res['data']['root'];
        this.warn_num=res['data']['total'];
      }else{
        this.notification.showAlert(res['message']); 
      } 
      this.notification.hideLoading(); 
    })
    //今日入库
    let nowin:Array<string>;
    let options2 = {
      start: 0,
      limit: PAGE_COUNT,
      //TO_DAYS(createDate) = TO_DAYS(NOW());  //to_days(addedTime) <= to_days(now()); 
      where_sql:"to_days(date_format(sp_in.in_date,'%Y-%m-%d %T')) = to_daYS(date_format(now(),'%Y-%m-%d %T')) and auditing_in = ?",
      where_type:'string',
      where_value:'1'
    };

    let nowi = this.api.getListPage('sp_in',options2).subscribe(res => {
      if(res['success']){
        nowin= res['data']['root'];
        this.now_in=res['data']['total'];
      }else{
        this.notification.showAlert(res['message']); 
      } 
      this.notification.hideLoading(); 
    })

    //今日出库
      let nowout :Array<string>;
      let options3 = {
        start: 0,
        limit: PAGE_COUNT,
        where_sql:"to_days(date_format(sp_out.out_date,'%Y-%m-%d %T')) = to_daYS(date_format(now(),'%Y-%m-%d %T')) and auditing_out= ?",
        where_type:'string',
        where_value:'1'
      };
      let nowo = this.api.getListPage('sp_out',options3).subscribe(res => {
        if(res['success']){
          nowout= res['data']['root'];
          this.now_out=res['data']['total'];
        }else{
          this.notification.showAlert(res['message']); 
        } 
        this.notification.hideLoading(); 
      })
  }
  
  //页面跳转详情页面
  storewarn(){
    //navCtrl页面跳转
    this.app.getRootNav().push('StoreWarnPage');
  }
  ionViewWillEnter(){
    this.chartOption = {
      backgroundColor:'#fff',
      series : [
          {
              name: '出库占比',
              type: 'pie',
              radius : '55%',
              center: ['50%', '60%'],
              data:[
                  {value:335, name:'五件套'},
                  {value:310, name:'枕头'},
                  {value:234, name:'枕套'},
                  {value:135, name:'抱枕'},
                  {value:154, name:'绒被'}
              ],
              itemStyle: {
                  emphasis: {
                      shadowBlur: 10,
                      shadowOffsetX: 0,
                      shadowColor: 'rgba(0, 0, 0, 0.5)'
                  }
              }
          }
      ]
  };
  }
 

  
  drawPieEchart(option) {
    this.pieChart = echarts.init(this.element.nativeElement.querySelector('#pieChart'));
    this.pieChart.setOption(option);
  }

  myInfo(){
    //navCtrl页面跳转
    this.app.getRootNav().push('UserinfoPage');
  }
}
